package com.example.nio.netty.session;

import io.netty.channel.Channel;
import java.util.HashMap;
import java.util.Map;

public class SessionUtil {

    private static Map<String, Channel> map = new HashMap<>();
    public static void bindSession(Channel channel, Session session) {
        map.put(session.getUserId(), channel);
        channel.attr(Attributes.attributeKey).set(session);
    }

    public static boolean hasLogin(Channel channel) {
        return channel.hasAttr(Attributes.attributeKey);
    }

    public static Channel getChannel(String toUserId) {
        return map.get(toUserId);
    }

    public static Session getSession(Channel channel) {
        return channel.attr(Attributes.attributeKey).get();
    }
}
